package com.deli.springboot.repository;

import com.deli.springboot.entity.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;


@Repository
public class CommentRepository {

    private final ConcurrentMap<Integer, Comment> repository = new ConcurrentHashMap<>();

    private final static AtomicInteger idGenerator = new AtomicInteger();

    /**
     * 保存对象
     *
     * @param comment
     * @return
     */
    public int add(Comment comment) {
        Integer id = idGenerator.incrementAndGet();
        comment.setId(id.longValue());
        if (repository.put(id, comment) != null) {
            return 0;
        } else {
            return 1;
        }
    }

    public List<Comment> selectByExample(Long commentId) {

        Collection<Comment> values = repository.values();
        ArrayList<Comment> comments = new ArrayList<>();
        for (Comment value : values) {
            if (commentId == value.getId()) {
//                Comment comment1 = new Comment();
//                comment1.setId(value.getId());
//                comment1.setBookId(value.getBookId());
//                comment1.setcDate(value.getcDate());
//                comment1.setContent(value.getContent());
//                comment1.setStatus(value.getStatus());
//                comment1.setUserId(value.getUserId());
//                comments.add(comment1);
                for (Comment comment : comments) {
                    BeanUtils.copyProperties(value, comment);
                }
            }
        }
        return comments;
    }
}
